{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1.函数\n",
    "\n",
    "## 1.函数的定义\n",
    "> 1. 数学 ： f(x) = ax+b\n",
    "> 2. python ： def fib(x) :\n",
    "> 3. 编程思维 VS 数学思维\n",
    ">> 1. 函数可以被定义名称,定义的关键字 <font style=\"color:red\">**def**</font>\n",
    ">> 2. 数学思维是写在同一行(定义函数 和 运算公式), 面向对象编程中 函数写多行, 定义函数为单独一行\n",
    ">> 3. 数学函数一般没有注释,而编程过程中,一般需要对函数进行注释描述(docstring,课本p151)\n",
    ">> 4. 数学中 : ax+b 是计算公式, 而编程中,我们将函数的内容成为**函数体**,函数体要有一定的**递进**\n",
    ">> 5. 数学函数执行,将参数代入公式,编程中函数执行,将参数代入函数体\n",
    "## 2.关于函数的定义更多\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 斐波那切数列: 0 1 1 2 3 5 8 13 21 34 \n",
    "def fib(x):\n",
    "     \"\"\"列出指定x以内的斐波那契数列\"\"\"\n",
    "     a,b = 0,1\n",
    "     while a < x :\n",
    "        print(a,end=\" \")\n",
    "        a,b = b, a+b\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1 1 2 3 5 8 13 21 34 55 89 "
     ]
    }
   ],
   "source": [
    "fib(100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实践L:请大家写一个函数,这个函数可以打印\"你好,XXX同学\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "def hello(name):\n",
    "  print(\"你好,%s同学~\"%(name))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "> 2. 调用函数/使用函数\n",
    ">> 1. 函数名(参数)\n",
    ">> 2. 参数可以不存在"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你好,zihao同学~\n"
     ]
    }
   ],
   "source": [
    "hello(\"zihao\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "def hello():\n",
    "   print(\"hello world\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world\n"
     ]
    }
   ],
   "source": [
    "hello()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 回顾与思考\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "# sleep() 需要给定一个时间(int)参数\n",
    "time.sleep(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2021, tm_mon=12, tm_mday=2, tm_hour=10, tm_min=45, tm_sec=53, tm_wday=3, tm_yday=336, tm_isdst=0)"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# localtime() 不需要给定参数\n",
    "time.localtime()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3.  函数的参数\n",
    "> 1. 形式参数\n",
    ">> 1. 在**定义**函数的时候定义的参数\n",
    ">> 2. 形式参数不占用内存\n",
    "> 2. 实际参数(实参)\n",
    ">> 1. 在**调用**函数的时候传递的参数\n",
    ">> 2. 调用函数时,形式参数分配空间,传递实际参数,执行函数内容"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "def hello(name):\n",
    "    print(\"你好,%s同学~%(name)\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "> 1. 形式参数: word\n",
    "> 2. 实际参数: \"sdadasdasdasd\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "def serch4vowels(word):\n",
    "    \"\"\"显示文本中出现的元音字母有哪些\"\"\"\n",
    "    vowels = set(\"aeiou\")\n",
    "    word = set(word)\n",
    "    print(vowels & word)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'a'}\n"
     ]
    }
   ],
   "source": [
    "serch4vowels(\"sdadasdasdasd\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "* 在多个参数的情境下\n",
    "\n",
    "> 1.位置参数\n",
    "\n",
    "> 2.关键字参数\n",
    "\n",
    "> 3.默认参数\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "def describe_pet(pet_name,animal_type):\n",
    "   \"\"\"显示宠物信息\"\"\"\n",
    "   print(\"\\nI have a \" + animal_type + \".\")\n",
    "   print(\"My\"  + animal_type + \"'s name is \"+pet_name.title() + \".\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have a cat.\n",
      "Mycat's name is 微积分.\n"
     ]
    }
   ],
   "source": [
    "describe_pet(animal_type=\"cat\",pet_name=\"微积分\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have a dog.\n",
      "Mydog's name is 豆豆.\n"
     ]
    }
   ],
   "source": [
    "describe_pet(\"豆豆\",\"dog\")                                                                                "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# * 参数注意事项实践\n",
    "> 1. 位置参数和关键字参数同时使用\n",
    ">> 1. positional argument follows keyword argument(关键字参数应该放在位置参数之后)\n",
    ">> 2. 只要满足1的条件,剩余的关键字参数(多个)不考虑先后顺序\n",
    "> 2.非默认参数和默认参数的同时使用(定义函数)\n",
    ">> 1. A.non-dafault argument follows default argument(默认参数均应该放在非默认参数之后)\n",
    ">> 2. 只要满足1的条件,剩余的默认参数(多个)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [],
   "source": [
    "def test(x,y,z):\n",
    "    \"\"\"打印X,Y,Z的结果\"\"\"\n",
    "    print(x)\n",
    "    print(y)\n",
    "    print(z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "positional argument follows keyword argument (<ipython-input-62-42371a9326bd>, line 2)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-62-42371a9326bd>\"\u001b[1;36m, line \u001b[1;32m2\u001b[0m\n\u001b[1;33m    test(5,y=3,2)\u001b[0m\n\u001b[1;37m              ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m positional argument follows keyword argument\n"
     ]
    }
   ],
   "source": [
    "# 实践1:\n",
    "test(5,y=3,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "3\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "#实践2\n",
    "test(5,3,z = 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "2\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "# 实践3:\n",
    "test(5,z=3,y=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "hide_input": false,
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
